package com.controller;


import com.constant.AppConstant;
import com.dto.JsonResult;
import com.dto.Page;
import com.dto.UserAndRoleDecn;
import com.dto.VersionDto;
import com.service.PageService;
import com.vo.PageVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class PageController {

    @Autowired
    private PageService pageService;

    //分页查询
    @PostMapping("/api/admin/page")
    public JsonResult listPageUsers(@RequestBody PageVo pageVo) {
        if (pageVo.getPageSize().equals("")) {
            Pageable pageable = PageRequest.of(Integer.parseInt(pageVo.getPage()) - AppConstant.DEFAULT_NUM, AppConstant.INIT_PAGE_SIZE);
            return JsonResult.buildSuccess(200, pageService.findAll(pageable));
        } else {
            Pageable pageable = PageRequest.of(Integer.parseInt(pageVo.getPage()) - AppConstant.DEFAULT_NUM, Integer.parseInt(pageVo.getPageSize()));
            return JsonResult.buildSuccess(200, pageService.findAll(pageable));
        }
    }

    //初始的分页查询默认情况下为第一页，10条数据
    @GetMapping("/api/admin/page/init")
    public JsonResult listPageUsersInit() {
        Pageable pageable = PageRequest.of(AppConstant.INIT_PAGE, AppConstant.INIT_PAGE_SIZE);
        Page<UserAndRoleDecn> all = pageService.findAll(pageable);
        all.setPageSize(AppConstant.INIT_PAGE_SIZE);
        //设置总页码
        return JsonResult.buildSuccess(200, all);
    }

    //初始的分页查询默认情况下为第一页，10条数据 条件为状态值,用户名
    @PostMapping("/api/admin/page/init/enable")
    public JsonResult listPageUsersInitEnable(@RequestBody PageVo pageVo) {
        Pageable pageable = PageRequest.of(AppConstant.INIT_PAGE, AppConstant.INIT_PAGE_SIZE);
        Page<UserAndRoleDecn> all = pageService.findAllByEnable(pageable, pageVo);
        all.setPageSize(AppConstant.INIT_PAGE_SIZE);
        //设置总页码
        return JsonResult.buildSuccess(200, all);
    }

    //初始的分页查询默认情况下为第一页，10条数据
    @GetMapping("/api/admin/page/version/init")
    public JsonResult listPageVersionInit() {
        Pageable pageable = PageRequest.of(AppConstant.INIT_PAGE, AppConstant.INIT_PAGE_SIZE);
        Page<VersionDto> all = pageService.findAllVersion(pageable);
        all.setPageSize(AppConstant.INIT_PAGE_SIZE);
        return JsonResult.buildSuccess(200, all);
    }

    //初始的分页查询默认情况下为第一页，10条数据 条件为状态值,用户名
    @PostMapping("/api/admin/page/version/words/init")
    public JsonResult listPageVersionInitEnable(@RequestBody PageVo pageVo) {
        Pageable pageable = PageRequest.of(AppConstant.INIT_PAGE, AppConstant.INIT_PAGE_SIZE);
        List<Page<VersionDto>> all = pageService.findAllVersionEnable(pageable, pageVo);
        //设置总页码
        return JsonResult.buildSuccess(200, all);
    }

    //分页查询更改页数
    @PostMapping("/api/admin/page/version/paging")
    public JsonResult listPageVersionPageNum(@RequestBody PageVo pageVo) {
        if (pageVo.getPageSize().equals("")) {
            Pageable pageable = PageRequest.of(Integer.parseInt(pageVo.getPage()) - AppConstant.DEFAULT_NUM, AppConstant.INIT_PAGE_SIZE);
            return JsonResult.buildSuccess(200, pageService.findAllVersion(pageable));
        } else {
            Pageable pageable = PageRequest.of(Integer.parseInt(pageVo.getPage()) - AppConstant.DEFAULT_NUM, Integer.parseInt(pageVo.getPageSize()));
            return JsonResult.buildSuccess(200, pageService.findAllVersion(pageable));
        }
    }

    //分页查询更改条数
    @PostMapping("/api/admin/page/version/pageSize")
    public JsonResult listPageVersionPageSize(@RequestBody PageVo pageVo) {
        Pageable pageable = PageRequest.of(AppConstant.INIT_PAGE, Integer.parseInt(pageVo.getPageSize()));
        return JsonResult.buildSuccess(200, pageService.findAllVersion(pageable));

    }

}
